﻿using System;
using System.Collections.Generic;

using System.Text;
using QQZiFramework;
using Newtonsoft.Json;
using System.ComponentModel;

namespace QQZiFramework.Membership
{
    /// <summary>
    /// 基础用户实体
    /// </summary>
    [DBTable(TableName = "A0_Users")]
    public class BaseUser : ITreeDBEntity, INotifyUser
    {
        /// <summary>
        /// 用户编号
        /// </summary>
        [DBField(Usage = DBFieldUsage.PrimaryKey, Identity = false)]
        public virtual int UserId { get; set; }

        /// <summary>
        /// 性别  1:男  2：女
        /// </summary>
        public int? Sex { get; set; }


        /// <summary>
        /// 微信号
        /// </summary>
        public string WeiXin { get; set; }

        public string QQ { get; set; }

        /// <summary>
        /// 昵称
        /// </summary>
        public string NickName { get; set; }


        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 头像地址，完整url
        /// </summary>
        public string HeadPicture { get; set; }
        /// <summary>
        /// 账号
        /// </summary>
        public string Account { get; set; }
        /// <summary>
        /// 手机号
        /// </summary>
        public string Mobile { get; set; }
        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        [JsonIgnore]
        public string Password { get; set; }
        /// <summary>
        /// 生日
        /// </summary>
        public string Birthday { get; set; }
        /// <summary>
        /// 身份证编号
        /// </summary>
        public string IDCard { get; set; }

        /// <summary>
        /// 创建日期
        /// </summary>
        [JsonConverter(typeof(UnixTimeConverter))]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 角色列表，程序中赋值（传值）
        /// </summary>
        public List<Role> Roles { get; set; }

        [DBField(Usage = DBFieldUsage.MarkDelete)]
        public virtual int? Status { get; set; }

        /// <summary>
        ///  最后登录时间
        /// </summary>
        [JsonConverter(typeof(UnixTimeConverter))]
        public DateTime LastLoginTime { get; set; }

        /// <summary>
        /// 微信同一主体的用户唯一标识符
        /// </summary>
        public string Unionid { get; set; }

        /// <summary>
        /// App终端号（登录和注销的时候更新）
        /// </summary>
        public string AppTermId { get; set; }

        /// <summary>
        /// 平台_小米推送regID
        /// </summary>
        public string PushId { get; set; }

        public int GetSex()
        {
            return Convert.ToInt32(IDCard.Substring(16, 1)) % 2;
        }

        public int GetAge()
        {
            return Convert.ToInt32(DateTime.Now.Year) - Convert.ToInt32(IDCard.Substring(6, 4));
        }

        public override string ToJson()
        {
            Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings() { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore };
            string pwd = this.Password;
            this.Password = null;
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(this, settings);
            this.Password = pwd;
            return json;
        }


        /// <summary>
        /// 推荐人Id（推荐人是代理商的情况）
        /// </summary>
        [DBField(Usage = DBFieldUsage.ForeignKey, ForeignTable = "A0_Users", ForeignProperty = "UserId")]
        public int? ReferenceUserID { get; set; }


    }


}
